package com.mdx.zhizhao.controller;

import com.mdx.zhizhao.domain.*;
import com.mdx.zhizhao.service.EnterpriseService;
import com.mdx.zhizhao.service.EvaluateService;
import com.mdx.zhizhao.service.JobService;
import com.mdx.zhizhao.service.SeekerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/evaluate" ,produces = {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
@CrossOrigin(origins = "http://localhost:5173")
public class EvaluateController {
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private SeekerService seekerService;
    @Autowired
    private JobService jobService;
    @PostMapping("/insertEvaluate")
    public Response<Evaluate> insertEvaluate(@RequestBody Evaluate evaluate){
        evaluateService.insertEvaluate(evaluate);
        Response<Evaluate> response = new Response<>();
        response.s(evaluate,"成功");
        return response;
    }
    @PostMapping("/selectBySeeker")
    public List<Evaluate> selectBySeeker(@RequestBody Evaluate evaluate){
        List<Evaluate> evaluates=evaluateService.selectBySeeker(evaluate.getSeekerid());
        for(Evaluate e:evaluates){
            System.out.println();
            String s1=e.getSeekerid();
            Seeker seeker=seekerService.selectSeeker(s1);
            e.setSeekername(seeker.getSeekername());
            String s3=e.getJobid();
            Job job=jobService.selectJob(s3);
            e.setJobname(job.getJobname());
            List<Enterprise> enterprises = new ArrayList<>();
            List<String>enterprisesIds=jobService.selectEnterpriseIdsByJobName(e.getJobname());
            if(!enterprisesIds.isEmpty()){
                for(String enterpriseId:enterprisesIds){
                    Enterprise enterprise=enterpriseService.selectEnterprise(enterpriseId);
                    Job job1=jobService.selectJobByJobNameAndEnterpriseId(e.getJobname(),enterpriseId);
                    Evaluate evaluate1=evaluateService.selectByEnterpriseIdAndJobId(enterprise.getEnterpriseid(),job1.getJobid(),e.getSeekerid());
                    evaluate1.setJoblocation(job1.getJoblocation());
                    enterprise.setEvaluate(evaluate1);
                    enterprises.add(enterprise);
                }
            }
            e.setEnterprises(enterprises);
        }
        return evaluates;
    }
}
